package com.example.topguli;

import java.util.concurrent.locks.ReentrantLock;

public class SynchorizedTest {
    private static Integer num=3000;
    private static ReentrantLock lock=new ReentrantLock();
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                while (true) {
                    lock.lock();
                    try {
//                        synchronized (SynchorizedTest.class){
                        if (num > 0) {
                            System.out.println(Thread.currentThread().getName() + "抢到了第" + num--);
                        } else {
                            break;
                        }
//                    }
                    } catch (Exception e) {

                    } finally {
                        lock.unlock();
                    }
                }
            }).start();
        }
    }
}
